<?php
/*
 * author: fabricio s costa
 * version: 1.0
 * site: http://fabriciojf.com
 */


class GerarFeed {


    function addChild($document, $node, $titulo, $link, $descricao) {
        $item = $document->createElement($node);
        $elemTitulo = $document->createElement('title', $titulo);
        $elemLink = $document->createElement('link', $link);
        $elemDescricao = $document->createElement('description', $descricao);

        $item->appendChild($elemTitulo);
        $item->appendChild($elemLink);
        $item->appendChild($elemDescricao);

        return $item;
    }

    public function gerarXml() {
        $dom = new DOMDocument('1.0', 'utf-8');
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;

        $root = $dom->createElement('rss');
	$channel = $dom->createElement('channel');
	$channel = self::addChild($dom, 'channel', 'Paulo J.', '(11) 5555-4444', 'Av. Principal, 80');

	$item1 = $dom->createElement('item');
	$item2 = $dom->createElement('item');

        #utilizando a funcao para criar contatos
        $item1 = self::addChild($dom, 'item', 'Paulo J.', '(11) 5555-4444', 'Av. Principal, 80');
        $channel->appendChild($item1);

        $item2 = self::addChild($dom, 'item', 'Joao S.', '(11) 4444-5555', 'R. da Feira, 70');
        $channel->appendChild($item2);

        $root->appendChild($channel);
        $dom->appendChild($root);
        $dom->save('agenda.xml'); #salvando o arquivo

        self::salvar($dom->saveXML()); #mostrar dados na tela
    }

    public function salvar($conteudo) {

	$arquivo = "feed.rss";
	$abrir = fopen($arquivo, "arquivotemp");
	$gravar = fwrite($abrir, $conteudo);
	$gravar;
    }

}
?>
